package com.h3c.web.support.console.impl;

import java.sql.SQLException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.h3c.dal.common.TableConstant;
import com.h3c.dal.dao.CasssoRoleDAO;
import com.h3c.dal.entity.CasssoRoleDO;
import com.h3c.dal.entity.query.CasssoRoleQuery;
import com.h3c.web.common.constant.SessionConstant;
import com.h3c.web.common.constant.RoleConstant.RoleEnum;
import com.h3c.web.common.utils.ListUtil;
import com.h3c.web.support.console.RoleSupport;

public class RoleSupportImpl implements RoleSupport {

	private CasssoRoleDAO casssoRoleDAO;

	@SuppressWarnings("unchecked")
	@Override
	public RoleEnum getRole(String roleId) throws SQLException {
		CasssoRoleQuery roleExample = new CasssoRoleQuery();
		roleExample.createCriteria().andRoleIdEqualTo(roleId).andStatusEqualTo(TableConstant.COMMON_STATUS_NORMAL);
		List<CasssoRoleDO> roleList = casssoRoleDAO.selectByExample(roleExample);

		if (ListUtil.isEmpty(roleList)) {
			return null;
		} else if (RoleEnum.super_admin.name().equals(roleList.get(0).getRoleCode())) {
			return RoleEnum.super_admin;
		} else if (RoleEnum.org_admin.name().equals(roleList.get(0).getRoleCode())) {
			return RoleEnum.org_admin;
		} else if (RoleEnum.ordinary_user.name().equals(roleList.get(0).getRoleCode())) {
			return RoleEnum.ordinary_user;
		}
		return null;
	}

	@Override
	public RoleEnum getRole(HttpServletRequest request) throws SQLException {
		String roleId = (String) request.getSession().getAttribute(SessionConstant.ROLE_ID);
		return getRole(roleId);
	}
	
	public CasssoRoleDAO getCasssoRoleDAO() {
		return casssoRoleDAO;
	}

	public void setCasssoRoleDAO(CasssoRoleDAO casssoRoleDAO) {
		this.casssoRoleDAO = casssoRoleDAO;
	}

	
}
